package action.listener;

import gui.Frame;
import gui.View;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

import undoredo.AbstractCommand;
import undoredo.EditEdgeCommand;

public class EditEdgeListener implements MouseListener
{
   private View view;
   private int x, y;

   public EditEdgeListener(View view)
   {
      super();
      this.view = view;
   }

   @Override
   public void mouseClicked(MouseEvent e)
   {
      // TODO Auto-generated method stub
   }

   @Override
   public void mouseEntered(MouseEvent e)
   {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseExited(MouseEvent e)
   {
      // TODO Auto-generated method stub

   }

   @Override
   public void mousePressed(MouseEvent e)
   {
      // TODO Auto-generated method stub
      x = e.getX();
      y = e.getY();
      int i = view.isEdge(x, y);

      if (i != -1)
      {
         String name = JOptionPane.showInputDialog("Enter the new Weight");
         try{

            int weight = Integer.parseInt(name);
            if (weight > 0)
            {
               //               view.getEdges().get(i).setWeight(weight);
               AbstractCommand command = new EditEdgeCommand(view.getEdges().get(i), weight);
               command.execute();

               Frame.addCommandToStack(command);
               Frame.setUndoRedoVisible();
               view.repaint();
            }
         }
         catch (NumberFormatException nf ){}

      }
   }

   @Override
   public void mouseReleased(MouseEvent e)
   {
      // TODO Auto-generated method stub

   }

}
